package org.iweb.html.api;

import java.lang.reflect.InvocationTargetException;

import javax.servlet.http.HttpServletRequest;

import org.iweb.html.impl.basic.Form.CustomFormBean;
import org.iweb.html.impl.js.engine.JavascriptEngine;

public class GroovyListener extends Listener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 799823779700874059L;

	//private String script;
	private Object scriptInstance;
	private String methodName;
	
	/*
	public GroovyListener(String id,String script,String methodName) {
		super(id);
		this.script=script;
		this.setMethodName(methodName);
	}
	
	public GroovyListener(String script) {
		super(null);
		this.script=script;
	}
	
	public GroovyListener(Serializable scriptInstance) {
		super(null);
		//this.script=script;
		this.scriptInstance=scriptInstance;
	}
	*/
	
	public GroovyListener(String id,Object scriptInstance,String methodName) {
		super(id);
		this.scriptInstance=scriptInstance;
		this.setMethodName(methodName);
	}

	@Override
	public void run(HttpServletRequest request, CustomFormBean formBean) {
		try {
			
			scriptInstance.getClass().getDeclaredMethod(getMethodName(),
					new Class[] {JavascriptEngine.class,CustomFormBean.class}).invoke(scriptInstance, new Object[] {getJsEngine(),formBean});
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}

	public String getMethodName() {
		return methodName;
	}

	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}

}
